package org.example.fronted.controller;


import org.example.entity.User;
import org.example.fronted.service.UserService1;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/front")
public class UserController1 {

    @Resource
    UserService1 userService1;

    @RequestMapping("/selectAllUser")
    public List<User> selectAllUser(){
        return userService1.selectAllUser();
    }

    /**
     * 登录
     * @param uName
     * @param uPassword
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public Map login(@RequestParam("username") String uName, @RequestParam("password") String uPassword,
                        HttpSession session){

        Map<String, Object> map = new HashMap<>();
        User user = userService1.login(uName, uPassword);
        if(user==null){
            System.out.println("user为null");
        }
        Integer uStatus = user.getUStatus();
        if(uStatus==0){
            System.out.println("status为0");
        }else {
            session.setAttribute("userId",user.getUId());
            Integer a = user.getUId();
            System.out.println(a);
            session.setAttribute("uName",user.getUName());

//            int uid=user.getUId();
//            ArrayList<Cart> myCart = (ArrayList<Cart>) session.getAttribute("myCart");

            map.put("code", 1);
            map.put("id",user.getUId());
            map.put("name",user.getUName());
            map.put("data","登陆成功");

        }
        return map;

    }



}
